package cn.springcloud.fix.auth.granter;

import cn.springcloud.fix.auth.dao.entity.User;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

public abstract class AbstractCustomTokenGranter extends AbstractTokenGranter {

    AbstractCustomTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, String grantType) {
        super(tokenServices, clientDetailsService, requestFactory, grantType);
    }

    @Override
    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
        Map<String, String> parameters = tokenRequest.getRequestParameters();
        User user = getCustomUser(parameters);
        // if (user == null) {
        //     throw new InvalidGrantException("无法获取用户信息");
        // }
        OAuth2Authentication authentication = super.getOAuth2Authentication(client, tokenRequest);
        authentication.setDetails(user);
        authentication.setAuthenticated(true);
        return authentication;
    }

    protected abstract User getCustomUser(Map<String, String> parameters);
}
